package com.jx.lcs.integral.service;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jx.lcs.integral.dao.UserIntegralDao;
import com.jx.lcs.integral.entity.UserIntegral;
import com.jx.lcs.util.export.Excel4MapExporter;
import com.jx.lcs.util.export.ExcelExporter;
import com.jx.lcs.util.export.SheetDefination;
import com.tianyu.jty.common.persistence.HibernateDao;
import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.service.BaseService;
import com.tianyu.jty.common.utils.DateUtils;

/**
 * 用户积分Service
 * 
 * @author JiaYY
 * @date 2015年8月25日
 */
@Service
@Transactional(readOnly = true)
public class UserIntegralService extends BaseService<UserIntegral, String> {

	@Autowired
	private UserIntegralDao userIntegralDao;

	@Override
	public HibernateDao<UserIntegral, String> getEntityDao() {
		return userIntegralDao;
	}

	/**
	 * 获取用户积分列表
	 * 
	 * @param page
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public Page<UserIntegral> findUserIntegralList(Page<UserIntegral> page,
			HttpServletRequest request) throws Exception {
		List<UserIntegral> result = userIntegralDao.findUserIntegralList(page,
				request);
		int totalCount = userIntegralDao.getUserIntegralCount(page, request);

		page.setResult(result);
		page.setTotalCount(totalCount);

		return page;
	}

	/**
	 * 获取用户积分详情
	 * 
	 * @param page
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public Page<UserIntegral> findUserIntegralDetail(Page<UserIntegral> page,
			HttpServletRequest request) throws Exception {
		String uid = request.getParameter("uid");
		String beginDate = request.getParameter("beginDate");
		String endDate = request.getParameter("endDate");
		if (!StringUtils.isBlank(endDate))
			endDate = DateUtils.addDays(endDate, 1);

		StringBuilder sb = new StringBuilder();
		sb.append("from UserIntegral t where t.uid='");
		sb.append(uid).append("'");

		if (!StringUtils.isBlank(beginDate)) {
			sb.append(" and t.recordTime >= '").append(beginDate).append("'");
		}
		if (!StringUtils.isBlank(endDate)) {
			sb.append(" and t.recordTime < '").append(endDate).append("'");
		}
		sb.append(" order by t.recordTime desc");

		return userIntegralDao.findPage(page, sb.toString(), new Object[] {});
	}

	public List<Object[]> addIntegralBetach() {
		return userIntegralDao.addIntegralBetach();
	}

	/**
	 * 用户积分数据导出
	 * @param request
	 * @param out
	 * @throws IOException
	 */
	public void exportUserIntegral(HttpServletRequest request,
			OutputStream out) throws IOException {

		//获客积分
		List<Map<String, Object>> customerIntegralData = userIntegralDao.findUserIntegralListByType(request, "0");

		SheetDefination<Map<String, Object>> prodSheetDefination = new SheetDefination<>();
		prodSheetDefination.setSheetTitle("获客积分");
		prodSheetDefination.setHeaders(new String[] { "员工号", "CRM编号", "姓名",
				"手机号", "营业部", "积分" });
		prodSheetDefination.setDataKeys(new String[] { "uid", "jxId",
				"userName", "telephone", "userDept",
				"incomeScore" });
		prodSheetDefination.setDataset(customerIntegralData);

		//关单积分
		List<Map<String, Object>> billIntegralData = userIntegralDao.findUserIntegralListByType(request, "1");

		SheetDefination<Map<String, Object>> userSheetDefination = new SheetDefination<>();
		userSheetDefination.setSheetTitle("关单积分");
		userSheetDefination.setHeaders(new String[] { "员工号", "CRM编号", "姓名",
				"手机号", "营业部", "积分" });
		userSheetDefination.setDataKeys(new String[] { "uid", "jxId",
				"userName", "telephone", "userDept",
				"incomeScore" });
		userSheetDefination.setDataset(billIntegralData);

		List<SheetDefination<Map<String, Object>>> definations = new ArrayList<>();
		definations.add(prodSheetDefination);
		definations.add(userSheetDefination);

		ExcelExporter<Map<String, Object>> exportExcel = new Excel4MapExporter();
		exportExcel.export(definations, out);
	}
	
	/**
	 * 获取用户积分列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public List<UserIntegral> findUserRestIntegralAll(HttpServletRequest request) throws Exception {
		return userIntegralDao.findUserRestIntegralList(request);
	}

}
